package com.example.demo.controller;

import com.example.demo.entity.Account;
import com.example.demo.service.AccountService;
import com.example.demo.util.ResponseHelper;
import com.example.demo.util.ResponseModel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 账号表(Account)表控制层
 *
 * @author makejava
 * @since 2020-12-09 00:39:57
 */
@Api(tags = "账号表(Account)")
@RestController
@RequestMapping("account")
public class AccountController {

    /**
     * 服务对象
     */
    @Resource
    private AccountService accountService;

    /**
     * 触发开始程序
     *
     * @return 是否成功
     */
    @GetMapping("start/{rateType}")
    @ApiOperation(value = "开始", notes = "触发开始程序")
    public ResponseModel start(@PathVariable Integer rateType) {
        accountService.start(rateType);
        return ResponseHelper.success(null);
    }

    /**
     * 新增/修改数据
     *
     * @param account 实例对象
     * @return 是否成功
     */
    @PostMapping("save")
    @ApiOperation(value = "新增/修改", notes = "新增/修改账号表的一条数据")
    @RequiresPermissions(logical = Logical.OR, value = {"account:insert", "account:edit"})
    public ResponseModel save(@RequestBody Account account) {
        return ResponseHelper.success(accountService.save(null, account));
    }

    /**
     * 通过主键删除数据
     *
     * @param id     主键
     * @return 是否成功
     */
    @DeleteMapping("delete/{id}")
    @ApiOperation(value = "删除单条数据", notes = "删除主键id的单条数据")
    @RequiresPermissions(logical = Logical.OR, value = {"account:delete"})
    public ResponseModel delete(@ApiParam(value = "账号表主键id", required = true) @PathVariable("id") Integer id) {
        return ResponseHelper.success(accountService.delete(null, id));
    }

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @GetMapping("queryById/{id}")
    @ApiOperation(value = "查询单条数据", notes = "通过id查询单条数据}")
    public ResponseModel queryById(@ApiParam(value = "账号表主键id", required = true) @PathVariable("id") Integer id) {
        return ResponseHelper.success(accountService.queryById(id));
    }

    /**
     * 查询多条数据
     *
     * @param account 查询条数
     * @return 对象列表
     */
    @PostMapping("list")
    @ApiOperation(value = "列表", notes = "查询账号表的多条数据")
    public ResponseModel list(@ApiParam(value = "账号表对象", required = true) @RequestBody Account account) {
        return ResponseHelper.success(accountService.list(null, account));
    }

}